﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using SimpleGameData.GameObjects;
using SimpleGameData.Animations;

namespace SimpleGame.SpriteClasses
{
    public class AnimatedSprite
    {
        #region Fields

        // The AnimatedSpriteData holds data read from an XML file.
        private AnimatedSpriteData animatedSpriteData;
        // The current animation to play for this animated sprite
        private SimpleGameData.Animations.AnimationKey currentAnimation;
        // If false the animation is paused
        private bool isAnimating;

        #endregion

        #region Properties

        /// <summary>
        /// Holds data read from an XML file.
        /// </summary>
        public AnimatedSpriteData AnimatedSpriteData
        {
            get { return animatedSpriteData; }
            set { animatedSpriteData = value; }
        }

        /// <summary>
        /// The current animation to play for this animated sprite.
        /// </summary>
        public SimpleGameData.Animations.AnimationKey CurrentAnimation
        {
            get { return currentAnimation; }
            set { currentAnimation = value; }
        }

        /// <summary>
        /// true = animation is playing or being updated
        /// false = animation is paused
        /// </summary>
        public bool IsAnimating
        {
            get { return isAnimating; }
            set { isAnimating = value; }
        }

        /// <summary>
        /// The width of the current animation frame.
        /// </summary>
        public int Width
        {
            get { return animatedSpriteData.Animations[currentAnimation].AnimationData.FrameWidth; }
        }

        /// <summary>
        /// The height of the current animation frame.
        /// </summary>
        public int Height
        {
            get { return animatedSpriteData.Animations[currentAnimation].AnimationData.FrameHeight; }
        }

        /// <summary>
        /// The Center of the current animation frame. i.e. used as rotation origin.
        /// </summary>
        public Vector2 SpriteCenter
        {
            get
            {
                return new Vector2(
                  (float)Width / 2f,
                  (float)Height / 2f);
            }
        }

        /// <summary>
        /// The Center of current animation frame plus the position of the frame.
        /// </summary>
        public Vector2 SpriteCenterPosition
        {
            get { return animatedSpriteData.Position + (SpriteCenter * animatedSpriteData.Scale); }
        }

        #endregion

        #region Constructor

        public AnimatedSprite(AnimatedSpriteData animatedSpriteData)
        {
            this.animatedSpriteData = animatedSpriteData;

            // initialize all the animations
            foreach (KeyValuePair<AnimationKey, Animation> pair in animatedSpriteData.Animations)
            {
                pair.Value.Initialize();
            }

            // make sure it's animating
            isAnimating = true;
        }

        #endregion

        #region Update & Draw Method

        public void Update(GameTime gameTime)
        {
            if (isAnimating)
            {
                animatedSpriteData.Animations[currentAnimation].Update(gameTime);
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(
                animatedSpriteData.Texture,
                SpriteCenterPosition,
                animatedSpriteData.Animations[currentAnimation].CurrentFrameRect,
                animatedSpriteData.TintColor,
                animatedSpriteData.Rotation,
                SpriteCenter,
                animatedSpriteData.Scale,
                SpriteEffects.None,
                0);
        }

        #endregion
    }
}
